from DeepSeek import DeepSeekAssistant, AudioDeviceError, TTSConversionError
from deepseek_model import ModelLoadError
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

def main():
    try:
        assistant = DeepSeekAssistant()
        assistant.initialize()
        logging.info("系统初始化完成，等待语音输入...")
        assistant.speak("请开始说话")

        while True:
            try:
                user_input = assistant.listen()
                if not user_input:
                    continue
                
                logging.info(f"用户输入: {user_input}")
                if "退出" in user_input:
                    break
                
                response = assistant.process_query(user_input)
                logging.info(f"生成回复: {response}")
                assistant.speak(response)
                
            except KeyboardInterrupt:
                logging.info("用户中断操作")
                break
            except TTSConversionError as e:
                logging.error(f"语音输出错误: {str(e)}")
                break
            except Exception as e:
                logging.error(f"运行时异常: {str(e)}")
                continue

    except ModelLoadError as e:
        logging.error(f"致命错误: {str(e)}")
        logging.error("建议检查：1. 网络连接 2. 模型路径 3. 磁盘空间")
    except AudioDeviceError as e:
        logging.error(f"硬件错误: {str(e)}")
        logging.error("建议检查：1. 麦克风连接 2. 音频输出设备 3. 系统权限")
    except Exception as e:
        logging.error(f"未处理的异常: {str(e)}")

if __name__ == "__main__":
    main()